from django.contrib.auth.models import User
from django import forms
from django.utils.translation import ugettext_lazy as _

import datetime

attrs_dict = {'class': 'required' }

class RegistrationForm(forms.Form):
	username = forms.RegexField ( regex=r'^[\w.@+-]+$',
					  max_length=30,
					  widget=forms.TextInput(attrs=attrs_dict),
					  label=_("Username"),
					  error_messages={'invalid': _("This value may only contain letters and numbers")})
	email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
					  maxlength=75)), label=_("Email"))
	first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
						maxlength=75)), label=_("First Name"))
	last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
						maxlength=75)), label=_("Last Name"))
	company = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
						maxlength=75)), label=_("Company"))
	password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
							    label=_("Password"))
	password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
								label=_("Password (again)"))

	# forms calls all clean_* methods automatically
	def clean_username(self):
		if User.objects.filter(name__iexact=name).exclude(
					pk=self.instance.user.pk).count() == 0:
			return self.cleaned_data['name']
		else:
			raise forms.ValidationError(_("A user with that username already existsy"))

	# run a check on the two passwords
	def clean(self):
		if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
			if self.cleaned_data['password1'] != self.cleaned_data['password2']:
				raise forms.ValidationError(_(u'Passwords do not match.'))
		return self.cleaned_data

# check out the email address
class RegistrationEmail(RegistrationForm):
	def clean_mail(self):
		if User.objects.filter(email__iexact=self.cleaned_data['email']):
			raise forms.ValidationError(_("This email is already in use."))
		return self.cleaned_data['email']

# this is the form used for registering a new conference; the user registering it is 
# automatically added as the conference manager
class ConferenceRegistration(forms.Form):
	name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
									maxlength=75)), label=_("Name"), required=True)
	company = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
									maxlength=75)), label=_("Company"), required=True)
	location = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict,
									maxlength=40)), label=_("Location"), required=True)
	public = forms.BooleanField(required=False)
	date = forms.DateField(initial=datetime.date.today)
	cost = forms.DecimalField(max_digits=5, decimal_places=2) 
	room = forms.DecimalField(max_digits=5, decimal_places=0)
	file = forms.FileField(required=False)
